package com.cpi.cornpp.salesforcast.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.exception.CustomerException;
import com.cpi.cornpp.customer.services.CustomerServices;
import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.salesforcast.exception.SalesForecastException;
import com.cpi.cornpp.salesforcast.form.SalesForecastForm;
import com.cpi.cornpp.salesforcast.view.SalesForecastView;

public class CustomerListAction extends MappingDispatchAction {
	private final static String DEFAULT = "default";//$NON-NLS-1$
	private static final String FAILURE = "fail";//$NON-NLS-1$
	private static final String SALES_FORECAST_IN_SESSION_LABEL = "SalesForecastView";//$NON-NLS-1$
	private static final String CUSTOMER_ID_LABEL = "customerId";//$NON-NLS-1$
	private static final String CUSTOMER_DESCRIPTION_LABEL = "customerDescription";//$NON-NLS-1$
	private static final String MATERIAL_ID_LABEL = "materialId";//$NON-NLS-1$
	private static final String MATERIAL_DESCRIPTION_LABEL = "materialDescription";//$NON-NLS-1$
	private static final String YEAR_LABEL = "year";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String MESSAGE_SELECT_A_CLIENT = "error.message.futurepurchase.add.customers";//$NON-NLS-1$
	private void saveFutureInSession(HttpServletRequest request){
		SalesForecastView salesForecastView = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession();
			salesForecastView = new SalesForecastView();
			salesForecastView.setCustomer(request.getParameter(CUSTOMER_ID_LABEL));
			salesForecastView.setCustomerDescription(request.getParameter(CUSTOMER_DESCRIPTION_LABEL));
			salesForecastView.setMaterial(request.getParameter(MATERIAL_ID_LABEL));
			salesForecastView.setMaterialDescription(request.getParameter(MATERIAL_DESCRIPTION_LABEL));
			salesForecastView.setFiscalYear(request.getParameter(YEAR_LABEL));
			httpSession.setAttribute(SALES_FORECAST_IN_SESSION_LABEL, salesForecastView);
		}finally{}
	}
	
	public ActionForward goCustomerSelection(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws CustomerException {
		SalesForecastForm salesForecastForm = null;
		String customerId = null;
		CustomerView customerView = null;
		Collection listOfCustomers = null;
		CustomerServices customerServices = null;
		MessageResources messageResources = null;
		String messageError = null;
		HttpSession httpSession = null;
		try{
			customerServices = new CustomerServices();
			salesForecastForm = (SalesForecastForm)form;
			messageResources = getResources(request);
			httpSession = request.getSession();
			this.saveFutureInSession(request);
			customerId = request.getParameter(CUSTOMER_ID_LABEL);
			customerView = new CustomerView();
			customerView.setCustomerId(StringUtils.EMPTY);
			if( StringUtils.isEmpty(customerId) ){
				customerView.setDescription(StringUtils.EMPTY);
				salesForecastForm.setCustomerViewSearch(customerView);
			}else{
				customerView.setDescription(customerId);
				salesForecastForm.setCustomerViewSearch(customerView);
				listOfCustomers = new ArrayList();
				listOfCustomers =  customerServices.getCustomersList(salesForecastForm.getCustomerViewSearch());
				request.setAttribute("listOfCustomers", listOfCustomers);
				salesForecastForm.setResultCustomerViews(listOfCustomers);
			}
		}catch (CustomerException customerException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), customerException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
				form.reset(mapping, request);
				throw customerException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw customerException;
		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward customerFutureSearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws SalesForecastException, CustomerException {
		SalesForecastForm salesForecastForm = null;
		CustomerServices customerServices = null;
		Collection listOfCustomers = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			salesForecastForm = (SalesForecastForm) form;
			customerServices = new CustomerServices();
			listOfCustomers = new ArrayList();
			listOfCustomers =  customerServices.getCustomersList(salesForecastForm.getCustomerViewSearch());
			request.setAttribute("listOfCustomers", listOfCustomers);
			salesForecastForm.setResultCustomerViews(listOfCustomers);
		}catch (CustomerException customerException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), customerException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
				form.reset(mapping, request);
				throw customerException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw customerException;
		}
		catch (Exception e){
			SalesForecastException salesForecastException=null;
			e.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward selectCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws SalesForecastException, CustomerException {
		SalesForecastForm salesForecastForm = null;
		CustomerView customerView = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		String forward = null;
		CustomerServices customerServices = null;
		try{
			messageResources = getResources(request);
			httpSession = request.getSession(false);
			salesForecastForm = (SalesForecastForm) form;
			if(salesForecastForm.getCustomerViewSearch().getCustomerId() != null){
				if (!CollectionUtils.isEmpty(salesForecastForm.getResultCustomerViews())){
					Iterator iterator = salesForecastForm.getResultCustomerViews().iterator();
					while (iterator.hasNext()) {
						CustomerView view = (CustomerView) iterator.next();
						if(org.apache.commons.lang.StringUtils.equals(view.getCustomerId().trim(), salesForecastForm.getCustomerViewSearch().getCustomerId().trim())){
							customerView = new CustomerView();
							customerView.setCustomerId(view.getCustomerId().trim());
							customerView.setDescription(view.getDescription().trim());
							httpSession = request.getSession(false);
							httpSession.setAttribute("CustomerView", customerView);
							forward = DEFAULT;
							break;
						}
					}
				}else{
					customerServices = new CustomerServices();
					customerView = customerServices.getCustomerById(salesForecastForm.getCustomerViewSearch().getCustomerId());
					httpSession.setAttribute("CustomerView", customerView);
					forward = DEFAULT;
				}
			}else{
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), MESSAGE_SELECT_A_CLIENT);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAILURE;
			}
		}catch (Exception e){
			SalesForecastException salesForecastException=null;
			e.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return mapping.findForward(forward);
	}
}